From: Álvaro Fernández Rojas Date: Mon, 10 Nov 2025 16:36:59 +0000 (+0100) Subject: blobmsg: refactor blobmsg_cast_u64/s64 X-Git-Url: http://git.openwrt.org/%22https:/collectd.org//%22/%22https:/collectd.org/%22?a=commitdiff_plain;h=a75209f62982f7218f73b9b4fd9b705e19f5f94a;p=project%2Flibubox.git blobmsg: refactor blobmsg_cast_u64/s64 Instead of calling blobmsg_type() for each if/else block, just call it once and use it with a switch. Link: https://github.com/openwrt/libubox/pull/24 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/blobmsg.h b/blobmsg.h index f2fc0d0..a34c4f7 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -305,32 +305,52 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr) static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr) { - uint64_t tmp = 0; + const int type = blobmsg_type(attr); + uint64_t tmp; - if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + switch (type) { + case BLOBMSG_TYPE_INT64: tmp = blobmsg_get_u64(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32) + break; + case BLOBMSG_TYPE_INT32: tmp = blobmsg_get_u32(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16) + break; + case BLOBMSG_TYPE_INT16: tmp = blobmsg_get_u16(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) + break; + case BLOBMSG_TYPE_INT8: tmp = blobmsg_get_u8(attr); + break; + default: + tmp = 0; + break; + } return tmp; } static inline int64_t blobmsg_cast_s64(struct blob_attr *attr) { - int64_t tmp = 0; + const int type = blobmsg_type(attr); + int64_t tmp; - if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + switch (type) { + case BLOBMSG_TYPE_INT64: tmp = blobmsg_get_u64(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32) - tmp = (int32_t)blobmsg_get_u32(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16) - tmp = (int16_t)blobmsg_get_u16(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) - tmp = (int8_t)blobmsg_get_u8(attr); + break; + case BLOBMSG_TYPE_INT32: + tmp = (int32_t) blobmsg_get_u32(attr); + break; + case BLOBMSG_TYPE_INT16: + tmp = (int16_t) blobmsg_get_u16(attr); + break; + case BLOBMSG_TYPE_INT8: + tmp = (int8_t) blobmsg_get_u8(attr); + break; + default: + tmp = 0; + break; + } return tmp; }